import 'package:flutter/material.dart';
import 'package:nft_shop/my/conteroller/my_write_conteroller.dart';
import 'package:nft_shop/res/global.dart';

import 'my_write_list_type_page.dart';

class MyWriteListPage extends StatefulWidget {
  final int index;
  const MyWriteListPage({Key? key,required this.index}) : super(key: key);

  @override
  _MyWriteListPageState createState() => _MyWriteListPageState();
}

class _MyWriteListPageState extends State<MyWriteListPage> with TickerProviderStateMixin,AutomaticKeepAliveClientMixin {
  ///page
  final PageController _pageController = PageController(initialPage: 0);
  /// 当前第几页
  int currentPageNum = 0;

  @override
  bool get wantKeepAlive => true;
  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        tabsWidget(),
        Expanded(
          child: Padding(
            padding: const EdgeInsets.symmetric(horizontal: 15,vertical: 5),
            child: PageView.builder(
              physics: new NeverScrollableScrollPhysics(),
              itemCount: 4,
              onPageChanged: (index){
                setState(() {
                  currentPageNum = index;
                });
              },
              controller: _pageController,
              itemBuilder: (_, index) {
                return MyWriteListTypePage(section: widget.index,row:index,);
              },
            ),
          ),
        ),
      ],
    );
  }

  ///选项卡
  Widget tabsWidget() {
    return Container(
        height: 30,
        width: 55*4,
        margin: EdgeInsets.only(top: 10,left: 15),
        decoration: BoxDecoration(
          color: Global.kTheme.colorBackground4,
          borderRadius: BorderRadius.circular(4),
        ),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: [
            InkWell(
              onTap: (){
                if (!mounted) {
                  return;
                }
                _jumpToPage(0);
              },
              child: Container(
                width: 55,
                decoration: BoxDecoration(
                  color: currentPageNum == 0?Global.kTheme.colorBackground5:Colors.transparent,
                  borderRadius: BorderRadius.circular(4),
                ),
                child: Center(
                    child: Text(
                      "全部",
                      style: TextStyle(
                          fontFamily: Global.mySemiboldFont,
                          fontSize: 12,
                          color: currentPageNum == 0?Global.kTheme.textPrimary1:Global.kTheme.textPrimary3),
                    )),
              ),
            ),
            InkWell(
              onTap: (){
                if (!mounted) {
                  return;
                }
                _jumpToPage(1);
              },
              child: Container(
                width: 55,
                decoration: BoxDecoration(
                  color: currentPageNum == 1?Global.kTheme.colorBackground5:Colors.transparent,
                  borderRadius: BorderRadius.circular(4),
                ),
                child: Center(
                    child: Text(
                      "藏品",
                      style: TextStyle(
                          fontFamily: Global.mySemiboldFont,
                          fontSize: 12,
                          color: currentPageNum == 1?Global.kTheme.textPrimary1:Global.kTheme.textPrimary3),
                    )),
              ),
            ),
            InkWell(
              onTap: (){
                if (!mounted) {
                  return;
                }
                _jumpToPage(2);
              },
              child: Container(
                width: 55,
                decoration: BoxDecoration(
                  color: currentPageNum == 2?Global.kTheme.colorBackground5:Colors.transparent,
                  borderRadius: BorderRadius.circular(4),
                ),
                child: Center(
                    child: Text(
                      "盲盒",
                      style: TextStyle(
                          fontFamily: Global.mySemiboldFont,
                          fontSize: 12,
                          color: currentPageNum == 2?Global.kTheme.textPrimary1:Global.kTheme.textPrimary3),
                    )),
              ),
            ),
            InkWell(
              onTap: (){
                if (!mounted) {
                  return;
                }
                _jumpToPage(3);
              },
              child: Container(
                width: 55,
                decoration: BoxDecoration(
                  color: currentPageNum == 3?Global.kTheme.colorBackground5:Colors.transparent,
                  borderRadius: BorderRadius.circular(4),
                ),
                child: Center(
                    child: Text(
                      "专辑",
                      style: TextStyle(
                          fontFamily: Global.mySemiboldFont,
                          fontSize: 12,
                          color: currentPageNum == 3?Global.kTheme.textPrimary1:Global.kTheme.textPrimary3),
                    )),
              ),
            ),
          ],
        ));
  }

  void _jumpToPage(int index){
    _pageController.jumpToPage(index);
    MyWriteConteroller.to.rowIndex = index;
    MyWriteConteroller.to.refresh();
  }

}
